﻿from functools import wraps
from google.appengine.api import users
from app.engine import models as models1
from app.forum import models
from django.http import HttpResponse, HttpResponseRedirect

def LoginUrl():
    user = users.get_current_user()
    if user:
        greeting = users.create_logout_url("/")
    else:
        greeting = users.create_login_url("/")
    return greeting

def ChkAuth():
    user = users.get_current_user()
    if user:
        try:
            member = models1.Members.all().filter('user =',user)
            if member.count() is 0:
                auth = 1
            else:
                auth = 0
        except AttributeError:
            auth = 0
    else:
        auth = 2
    return auth

def ChkAdmin2():
	user = users.get_current_user()
	if user:
		if  users.is_current_user_admin():
			admin = 0
		else:
			admin = 1
	else:
		admin = 1
	return admin

def ChkAdmin():
	user = users.get_current_user()
	keyname='wow-dkp-engine'
	userlist = models1.SiteConfig.get_or_insert(keyname)
	if users.is_current_user_admin():
		admin = 0
	return admin

def login_required(func):
    def _wrapper(request, *args, **kw):
        user = users.get_current_user()
        if user:
            return func(request, *args, **kw)
        else:
            return HttpResponseRedirect(users.create_login_url(request.get_full_path()))
    return _wrapper

def admin_required(func):
    def _wrapper(request, *args, **kw):
        if users.is_current_user_admin():
            return func(request, *args, **kw)
        else:
            return HttpResponseRedirect(users.create_login_url(request.get_full_path()))
    return _wrapper

def member_required(func):
    def _wrapper(request, *args, **kw):
        user = users.get_current_user()
        member = models.Members.all().filter('user =', user)
        if member.count() > 0:
            return func(request, *args, **kw)
        else:
            return HttpResponseRedirect("/forum/register/")
    return _wrapper
